package com.xinnet.mms.web;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xinnet.mms.allot.service.CustomAllotConfigService;
import com.xinnet.mms.area.service.AreaService;
import com.xinnet.mms.company.service.CompanyService;
import com.xinnet.mms.dept.service.DeptService;
import com.xinnet.mms.entity.Area;
import com.xinnet.mms.entity.Company;
import com.xinnet.mms.entity.Dept;
import com.xinnet.mms.entity.Emp;
import com.xinnet.mms.entity.Product;
import com.xinnet.mms.product.service.ProductService;

@Controller
@RequestMapping(value = "/authority/department")
public class DepartmentController {
	@Autowired
	private DeptService deptService;
	@Autowired
	private AreaService areaService;
	@Autowired
	private ProductService productService;
	@Autowired
	private CustomAllotConfigService customAllotConfigService;
	
	@Autowired
	private CompanyService companyService;
	
	/**
	 * 跳转到部门页面
	 * @param request
	 * @param response
	 * @return
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@RequestMapping(value = "/departmentPage")
	public String departmentPage(HttpServletRequest request,HttpServletResponse response){
		String deptJson  = deptService.getDeptTree();
		response.setContentType("text/html;charset=UTF-8;pageEncoding=UTF-8");
		response.setCharacterEncoding("UTF-8");
		request.setAttribute("deptJson", deptJson);
		List<Dept> deptList = deptService.getDeptList();
		request.setAttribute("deptList", deptList);
		List<Area> areaList = areaService.getAreaList();

		List<Area> parentAreaList = areaService.getAreaOfClassA();
		request.setAttribute("parentAreaList", parentAreaList);
		request.setAttribute("areaList", areaList);
		List<Product> productList = productService.queryEnableProduct();
		List<Company> companyList= companyService.getAllCompanyList();
		request.setAttribute("companyList", companyList);
		request.setAttribute("productList", productList);
		String result = "/authority/department";
		return result;
	}
	/**
	 * 判断部门名称是否可以使用
	 * @param request
	 * @param response
	 * @return
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@ResponseBody
	@RequestMapping(value = "/isCanUseTheDeptName")
	public String isCanUseTheDeptName(HttpServletRequest request,HttpServletResponse response){
		String id = request.getParameter("id");
		String isUpdate = request.getParameter("isUpdate");
		String dname = request.getParameter("dname");
		String isCanUseTheDeptName = deptService.isCanUseTheDeptName(isUpdate,dname,id);
		return isCanUseTheDeptName;
	}
	/**
	 * 添加部门
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException 
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@ResponseBody
	@RequestMapping(value = "/addDepartment")
	public String addDepartment(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		String areaStr = request.getParameter("areaStr");
		String productStr = request.getParameter("productStr");
		String companyIdStr = request.getParameter("companyStr");
		String parentId = request.getParameter("parentId");
		String dname = request.getParameter("dname");
		Dept dept = new Dept();
		dept.setDname(dname);
		dept.setParentId(parentId);
		dept.setCompanyIdStr(companyIdStr);
		deptService.addDepartment(dept,areaStr,productStr);
		String deptId = dept.getId();
		customAllotConfigService.addDeptDsitributRule(deptId);
		return "success";
	}
	/**
	 * 删除部门
	 * @param request
	 * @param response
	 * @return
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@ResponseBody
	@RequestMapping(value = "/delDepartment")
	public String delDepartment(HttpServletRequest request,HttpServletResponse response){
		String deptId = request.getParameter("deptId");
		if("0".equals(deptId)){
			return "0";
		}
		String result = deptService.delDeptById(deptId);
		return result;
	}
	/**
	 * 修改部门时，获取部门
	 * @param request
	 * @param response
	 * @return
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@ResponseBody
	@RequestMapping(value = "/getDeptById")
	public Dept  getDeptById(HttpServletRequest request,HttpServletResponse response){
		String deptId = request.getParameter("deptId");
		Dept dept = deptService.getDeptByDeptId(deptId);
		return dept;
	}
	/**
	 * 更新部门
	 * @param request
	 * @param response
	 * @return
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping(value = "/updateDepartment")
	public String updateDepartment(HttpServletRequest request,HttpServletResponse response){
		String id = request.getParameter("id");
		String areaStr = request.getParameter("areaStr");
		String productStr = request.getParameter("productStr");
		String companyIdStr = request.getParameter("companyStr");
		String parentId = request.getParameter("parentId");
		String dname = request.getParameter("dname");
		Dept dept = new Dept();
		dept.setId(id);
		dept.setDname(dname);
		dept.setParentId(parentId);
		dept.setCompanyIdStr(companyIdStr);
		deptService.updateDept(dept,areaStr,productStr);
		return "/authority/department";
	}
	
	/**
	 * 查看时候有员工使用此部门，在更新或删除时，如果有员工使用此部门，则不能更新或删除。
	 * @param request
	 * @param response
	 * @return
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@ResponseBody
	@RequestMapping(value = "/isCanUpdateDept")
	public String isCanUpdateDept(HttpServletRequest request,HttpServletResponse response){
		String deptId = request.getParameter("deptId");
		if("0".equals(deptId)){
			return "false";
		}
		Emp emp = deptService.getEmpListByDeptId(deptId);
		if(emp!=null){
			return "0";
		}
		List<Dept> deptList =  deptService.getDeptByParentId(deptId);
		if(deptList!=null && deptList.size()>0){
			return "2";
		}
		return "true";
	}
	
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@ResponseBody
	@RequestMapping(value = "/getAllArea")
	public List<Area> getAllArea(HttpServletRequest request,HttpServletResponse response){
		List<Area> areaList = areaService.getAreaOfClassB();
		return areaList;
	}
	
	/**
	 * 
	 *
	 * 功能描述：同步部门关系
	 * 
	 * @author 湛智
	 *
	 * @throws 2014年7月17日
	 *
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping(value = "/sysnDeptRelation")
	public String sysnDeptRelation() throws Exception {
		deptService.sysnDeptRelation();
		return "/authority/department";
	}
	
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@ResponseBody
	@RequestMapping(value = "/getDeptByString")
	public List<Dept> getDeptByName() throws Exception {
		List<Dept> areaList =deptService.getDeptByString();
		return areaList;
	}
}
